const express = require('express')
const app = express()
const fs = require("fs");
const cors = require("cors");
const path = require("path")
const compression = require('compression')
const https = require('https')

const httpPort = 10058
const httpsPort = 10070;

const httpsOption = {
    key: fs.readFileSync("./https/2_weipiaopiao.top.key"),
    cert: fs.readFileSync("./https/1_weipiaopiao.top_bundle.crt")
}

//跨域处理
app.use(cors({
    "origin": "*",
    "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
    "preflightContinue": false,
    "optionsSuccessStatus": 204
}))


//挂载gzip压缩,这里必须要在挂载静态资源之前
app.use(compression())


//挂载静态文件目录
app.use(express.static(path.join(__dirname, '/public')));




app.get('/getbg', (req, res) => {
    const files = fs.readdirSync('./public/bg')

    for (let i = 0; i < files.length; i++) {
        files[i] = '/bg/' + files[i]
    }


    res.send(files)
})


app.listen(httpPort, () => console.log(`Server running at :${httpPort}`))
https.createServer(httpsOption, app).listen(httpsPort, function() {
    console.log(`Https server listening on port ${httpsPort}`);


});